import Taro from '@tarojs/taro'
import { HTTP_STATUS } from './status'


var config=[]
var nconfig=[]
var baseurl = 'https://xjzbsmall.g2ma.com'
var weblogo='https://minichat.g2ma.com/kedouimg/xjlogo.png'
var appmark='xjzb'
var webname='物格码世界' 

// var weblogo='https://minichat.g2ma.com/kedouimg/A6984900727459logo.png'
// var webname='蝌蚪社群'
//  var config=[]
//  var nconfig=[]
//  var appmark='kedou'
//  var baseurl = 'https://minichat.g2ma.com'
     
//
//  var weblogo='http://storage.g2ma.com/upload/images/project/img_20200821162613132.png'
//  var webname='烯热'
//  var config=[]
//  var nconfig=[]
//  var appmark='kedou'
//  var baseurl = 'https://minichat.g2ma.com'
//  Taro.setStorage({key:'noshow',data:5})


var COMMONJS={
	getshow(){
		return 12
	},
	geturl(){
		return baseurl
	},
	getname(){
		return webname
	},
	getlogo(){
		return weblogo
	},
	getmark(){
		return appmark
	},
	open(url){
		var  index=config.indexOf(url);
		if(index>=0){
			url=nconfig[index]
		}
		Taro.redirectTo({
		  url:url
		})
	},
	openTo(url){
		var  index=config.indexOf(url);
		if(index>=0){
			url=nconfig[index]
		}
		Taro.navigateTo({
			url:url
		})
	},
	countDowns(dates, times,type){
	    //倒计时处理
	    var day = 0
	    var hour = 0
	    var minute = 0
	    var second = 0//时间默认值
	    if (times > 0) {
	      var timest = (times - dates)/1000
	      // console.log(JSON.stringify(timest)+'xxxxxxxxx')
	      day = Math.floor(timest / (60 * 60 * 24))
	      hour = Math.floor(timest / (60 * 60)) - (day * 24)
	      minute = Math.floor(timest / 60) - (day * 24 * 60) - (hour * 60)
	      second = Math.floor(timest) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60)
	    }
	    if(type==1){
	    	if(day>0){
	    		var tcon = day+'天'+hour+'小时'+minute+'分钟'
	    	}
	    	else if(hour>0){
	    		var tcon = hour+'小时'+minute+'分钟'
	    	}
	    	else{
	    		var tcon = minute+'分钟'
	    	}
	    }
	    else{
	    	var tcon = day+'天'+hour+'小时'+minute+'分钟'+second+'秒'
	    }
	    return tcon
	},
	msg(val){
		Taro.showToast({
		  title:val,
		  icon: 'none',
		  duration: 2000
		})
	},
	onShareAppMessage(title, path, imageUrl){
		return {
	      title: title,
	      path: path,
	      imageUrl: imageUrl
    	}
	},
	uploadFile(url, filePath, name, formData, callback) {
		Taro.uploadFile({
	      url: url,
	      filePath: filePath,
	      name: name,
	      header: {
	        'content-type': 'multipart/form-data'
	      },
	      formData: formData,
	      success:function(res) {
	      	if(res){
				var ret=res.data;
				if(ret.code!=300){
					callback(ret)
				}
				else{
					Taro.showToast({
					  title:ret.msg,
					  icon: 'none',
					  duration: 2000
					})
				}
			}
	      },
	      fai:function(){
	      	Taro.showToast({
			  title:'网络异常',
			  icon: 'none',
			  duration: 2000
			})
	      }
	    })
	},
	FormatDate(strTime,type) {
	    var Mytime="";
	    var date = new Date(strTime);
	    var myYear = date.getFullYear();
	    var myMonth = (date.getMonth()+1);
	    var myDate = date.getDate();
	    var myHours = date.getHours();
	    var myMinutes = date.getMinutes();
	    if(myMinutes<10){
	        myMinutes="0"+myMinutes;
	    }
	    if(type==1){
	         Mytime =myDate+" "+myHours+":"+myMinutes;
	    }
	    else if(type==2){
	         Mytime =myYear+"-"+myMonth+"-"+myDate;
	    }
	    else{
	         Mytime = myYear+"-"+myMonth+"-"+myDate+" "+myHours+":"+myMinutes;
	    }
	    return Mytime;
	},
	getFootPrint(type,id,entityId,uc){
		/*轨迹采集脚本*/
		/*type:节点状态：进入（查看），离开 = ['COME', 'GO'],*/
		/*entityId:商品则是商品Id，活动则是活动id,没有就传0*/
		/*id:1-蝌蚪社群首页，2-活动，3-抽奖，4-商品，5-优惠券，6-积分商品券*/
		var url="/crmweb/papi/v1.9/customer-shop/footPrint";
		var dates={
		  "entityId":entityId,
		  "nodeStatus":type,
		  "footPrintType": {
		    "id":id,
		  }
		}
		if(id==4&&type=='COME'){
			if(uc=='WUC'){
				dates.commodityType=3
			}
			else if(uc=='UC'){
				dates.commodityType=2
			}
			else{
				dates.commodityType=1
			}
		}
		this.ajaxRequest(url,dates,'POST',function(res){},"wfw","noret")
	},
	ajaxRequest(url,date,method,callback,type,types,typeis) {
		var contentType="";
		var token="";
		var dateinfo=Taro.getStorageSync('dateinfo')
		if(dateinfo){
			if(dateinfo.token){
				token=dateinfo.token
			}
		}
		if(type!='noloading'){
			Taro.showLoading({title: '加载中'})
		}
		const accountInfo = Taro.getAccountInfoSync();
		const appid=accountInfo.miniProgram.appId;
		var  headerinfo={
			"token":token,
			"AuthAppid":appid
		}
		if(type=="wfw"){
			/*微服务接口调用方式跟之前的不一致*/
				
			if(token){
				var  headerinfo={
					"Authorization":"Bearer "+token,
					"AuthAppid":appid
				}
			}
		}
		
		var _this=this
		Taro.request({
			url: baseurl + url,
			header:headerinfo,
			data: date,
			method: method,
			dataType:'json',
			success:function(res) {
				Taro.hideLoading()
				if(res){
					var ret=res.data;
					if(types!='noret'){
						if(ret.code==401){
							_this.open("/pages/login/login")
						}
						else if(ret.code!=300&&ret.code!=401){
							if(type=="wfw"){
								if(ret.success){
									setTimeout(function(){
										callback(ret)
									},300)
								}
								else{
									if(ret.message=="token已过期或不正确"||ret.message=='请先登录。'){
										_this.open("/pages/login/login")
									}
									else{
										setTimeout(function(){
											Taro.showToast({
											  title:ret.message,
											  icon: 'none',
											  duration: 2000
											})
										},300)
									}
								}
							}
							else{
								setTimeout(function(){
									callback(ret)
								},300)
							}
						}

						else{
							
							Taro.showToast({
							  title:ret.msg,
							  icon: 'none',
							  duration: 2000
							})
						}
					}
					else{
						setTimeout(function(){
							callback(ret)
						},300)
					}
				}
				else{
					
					Taro.showToast({
					  title:ret.msg,
					  icon: 'none',
					  duration: 2000
					})
				}
			},
			fail:function(){
				Taro.hideLoading()
				Taro.showToast({
				  title:'网络异常',
				  icon: 'none',
				  duration: 2000
				})
			},
			complete:function(){
//				if(typeis!='hideLoad'){
//					Taro.hideLoading()
//				}
			}
		})
	}

}
export default COMMONJS;
